import '../base/network/api_request.dart';

///
/// Description: 登录
///

/// 账号密码 登录
const _apiLogin = "/api-domain/user-center/authority/authority/v1/login";

///手机号登录
const _apiMobileLogin =
    "/api-domain/user-center/authority/authority/v1/login/bySms";

///手机号登录验证码
const _apiMobileLoginSms =
    "/api-domain/user-center/authority/authority/v1/login/sms";

class LoginRepository {
  Future<NDUser> accountLogin(
      {String? account, String? password, bool forceLogin = false}) async {
    var params = {
      'account': account,
      'password': password,
      'isForce': forceLogin ? 1 : 0,
    };
    Api.shareInstance().addHeader({'agent': 'APP'});
    var result =
        await Api.shareInstance().post(_apiLogin, data: {'body': params});
    if (result.code == 200 && result.data['code'] == 200) {
      var user = NDUser.fromJson(result.data!['data']['entity']);
      return Future.value(user);
    } else {
      return Future.error(
          '${result.data['code'] ?? result.code}:${result.data['message'] ?? result.message}');
    }
  }

  Future<NDUser> mobileLogin(
      {String? mobile, String? code, bool forceLogin = false}) async {
    var params = {
      'mobilePhone': mobile,
      'smsCode': code,
      'isForce': forceLogin ? 1 : 0,
    };
    Api.shareInstance().addHeader({'agent': 'APP'});
    var result =
        await Api.shareInstance().post(_apiMobileLogin, data: {'body': params});
    if (result.code == 200 && result.data['code'] == 200) {
      var user = NDUser.fromJson(result.data!['data']['entity']);
      return Future.value(user);
    } else {
      return Future.error(
          '${result.data['code'] ?? result.code}:${result.data['message'] ?? result.message}');
    }
  }

  Future<bool> mobileLoginSms({String? mobile}) async {
    var params = {'phoneNum': mobile};

    var result = await Api.shareInstance().post(
      _apiMobileLoginSms,
      data: {'body': params},
    );
    if (result.code == 200 && result.data['code'] == 200) {
      return Future.value(true);
    } else {
      return Future.error(
          '${result.data['code'] ?? result.code}:${result.data['message'] ?? result.message}');
    }
  }
}

class NDUser {
  ///租户id
  String? tenantId;

  ///员工id
  int? staffId;

  ///员工姓名
  String? staffName;

  ///公司名称
  String? companyName;

  ///登录手机号
  String? mobilePhone;

  ///推送channel 集合
  List<String>? channels;

  bool? needResetPassword;

  String? resetPasswordTip;

  /// 是否可以免费试用  用于控制显示 试用绑定手机号 引导浮层提示
  bool? canFreeTrial;
  String? secretKey;

  NDUser();

  factory NDUser.fromJson(Map<String, dynamic> json) => NDUser()
    ..tenantId = json['tenantId'] as String?
    ..staffId = json['staffId'] as int?
    ..staffName = json['staffName'] as String?
    ..companyName = json['companyName'] as String?
    ..mobilePhone = json['mobilePhone'] as String?
    ..channels =
        (json['channels'] as List<dynamic>?)?.map((e) => e as String).toList()
    ..needResetPassword = json['needResetPassword'] as bool?
    ..resetPasswordTip = json['resetPasswordTip'] as String?
    ..canFreeTrial = json['canFreeTrial'] as bool?
    ..secretKey = json['secretKey'] as String?;

  Map<String, dynamic> toJson() {
    return <String, dynamic>{
      'tenantId': tenantId,
      'staffId': staffId,
      'staffName': staffName,
      'companyName': companyName,
      'mobilePhone': mobilePhone,
      'channels': channels,
      'needResetPassword': needResetPassword,
      'resetPasswordTip': resetPasswordTip,
      'canFreeTrial': canFreeTrial,
      'secretKey': secretKey,
    };
  }

  @override
  bool operator ==(Object other) => (other is NDUser)
      ? staffId == other.staffId && tenantId == other.tenantId
      : false;

  @override
  int get hashCode => staffId ?? super.hashCode;
}
